package cn.zb.controller;

import cn.zb.api.flow.dto.FlowDTO;
import cn.zb.common.util.RedisUtil;
import cn.zb.common.util.Result;
import cn.zb.entity.auth.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.dromara.warm.flow.core.dto.FlowParams;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.enums.SkipType;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.core.service.InsService;
import org.dromara.warm.flow.orm.entity.FlowDefinition;
import org.dromara.warm.flow.orm.entity.FlowInstance;
import org.dromara.warm.flow.orm.mapper.FlowDefinitionMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("flow")
@Log
@Api("流程管理")
public class FlowController {
    @Resource
    InsService insService;
    @Resource
    DefService defService;
    @Resource
    FlowDefinitionMapper definitionMapper;

    @PostMapping("list")
    @ApiOperation("查询所有流程图部署")
    public Result<IPage<FlowDefinition>> list(@RequestBody FlowDTO dto){
        // 条件构造
        QueryWrapper<FlowDefinition> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dto.getFlowcode()),"flow_code",dto.getFlowcode());
        qw.like(StringUtils.isNotBlank(dto.getFlowname()),"flow_name",dto.getFlowcode());
        qw.eq(dto.getFb()!=null,"is_publish",dto.getFb());
        IPage<FlowDefinition> page = new Page<>(dto.getPage(),dto.getSize());
        definitionMapper.selectPage(page,qw);
        return Result.data(page);
    }


    // 请假申请
    @RequestMapping("qj")
    public String addQj(@RequestBody Map<String, String> map){
        FlowParams test = FlowParams.build().flowCode("test");
        // 设置创建者
        test.handler(map.get("yhm"));
        test.message(map.get("msg"));
        test.ext(map.get("msg"));
        // 开启流程实例
        Instance instance = insService.start(UUID.randomUUID().toString(), test);
        // 直接流转下一个节点
        FlowParams user = FlowParams.build()
                .skipType(SkipType.PASS.getKey());
        Map<String,Object> cs = new HashMap<>();
        cs.put("day",map.get("day"));
        user.variable(cs);

        insService.skipByInsId(instance.getId(),user);
        return "申请成功";
    }

    // 查询我的所有任务
    @RequestMapping("rw")
    public List rw(@RequestBody Map<String, String> map){
        FlowInstance instance = new FlowInstance();
        instance.setCreateBy(map.get("yhm"));
        return insService.list(instance);
    }

    // 根据实例id查询任务流程图
    @RequestMapping("rwlct")
    public String rwlct(Long id) throws IOException {
        String s = defService.flowChart(id);
        return "data:image/png;base64,"+s;
    }
    @Resource
    RedisUtil redisUtil;
    @RequestMapping("test")
    @PreAuthorize("hasRole('超级管理员')")
    public String s(@RequestHeader("token") String token){
        log.info("测试日志");
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        System.out.println(user);
        return "测试日志";
    }
}

